var vue = new Vue({
    el: '#app',
    data: {
        loading: false,
        loadingRole: false,
        useRootOrgId: Util.getParam('useRootOrgId') || '0',
        encryptTel: Util.getParam('encryptTel') || '0',
        tableData: [],
        tableDataRole: [],
        selectionKeys:[],
        user:'',
        // tableDataRole2:[],
        menuTreeData: [],
        paramData: {
            account: '',
            phone: '',
            username: '',
            currentPage: 1, //当前页数
            pageSize: 10, //每页条数
            total: 0 //总条数
        },
        paramDataRole: {
            roleType: Util.getParam('roleType') || '',
            authorized: true,
            roleName: ''
        },
        statusDic: {'0': '编辑', '1': '有效', '2': '无效'},
        roleTypeDic: {'ORG_PUB_R': '内置机构', 'ENT_PUB_R': '内置企业', 'FLOW_R': '内置流程'},
        selectUser: '',
        multipleSelection: [],
        dialogRoleMenuVisible: false,
        treeProps: {
            children: 'children',
            label: 'name',
            disabled: 'disabled'
        },
        activeNames: ['1'],
        loginUser: Util.getLoginUser(),
    },
    created: function () {
        this.initTable();
        this.initTableRole();
        // this.initTree();
    },
    methods: {
        handleSizeChange: function (pageSize) {
            this.paramData.pageSize = pageSize;
            this.initTable();
        },
        handleCurrentChange: function (currentPage) {
            this.paramData.currentPage = currentPage;
            this.initTable();
        },

        onSearch: function () {
            this.paramData.currentPage = 1;
            this.initTable();
        },
        onSearchRole: function () {
            this.initTableRole();

        },
        userSelect: function (row) {
            var self = this;
            this.user = row;
            console.log(this.user)
            this.multipleSelection=[]
            Util.post('/sysUser/userRoleByUserId/'+row.id,null, function (result) {
                console.log(result.data)
                let arr2 = result.data.map(key => key.roleId)
                // this.selectionKeys = arr2
                // let tableDataRole = this.tableDataRole
                if (result.success) {
                    // this.multipleSelection = arr2
                    console.log(this.multipleSelection)
                    // const {selectionKeys,tableDataRole} = this
                    arr2.forEach(key => {
                        this.tableDataRole.forEach(row => {
                            if (row.id == key) {
                                this.$refs.roleTable.toggleRowSelection(row, true);
                            }
                        })
                    })
                } else {
                    self.$message.error(result.msg);
                }
            });

        },
        handleSelectionChange: function (row) {
            this.multipleSelection = row;

        },
        save: function () {
            var self = this;
            // var sysRoles = [];
            // for (var i = 0; i < self.multipleSelection.length; i++) {
            //     sysRoles.push(self.multipleSelection[i].id);
            // }
            // console.log(sysRoles)
            if(this.user.length==0){
                self.$alert("请选择用户!")
            }else {
                console.log(self.multipleSelection)
                self.$confirm('确定授权?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    self.updateUserRole({
                        user:self.user,
                        sysRoles: self.multipleSelection
                    });
                }).catch(function () {
                    self.$message({type: 'info', message: '取消操作!'});
                });
            }
        },
        updateUserRole: function (paramData) {
            console.log(paramData)
            var self = this;
            Util.postJson('/sysUser/userRoleCommit', paramData, function (result) {
                if (result.success) {
                    self.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                } else {
                    self.$message.error(result.msg);
                }
            });
        },
        // loadUserRole: function () {
        //     var self = this;
        //     Util.postEncrypt('/system/rightuser/listUserRoles',
        //         {userId: self.selectUser.userId, orgId: (self.useRootOrgId === '1' ? self.selectUser.rootOrgId : self.selectUser.orgId)}, function (result) {
        //             if (result.success) {
        //                 var roleIds = result.obj.roleIds || [];
        //                 for (var k = 0, scnt = roleIds.length; k < scnt; k++) {
        //                     var rid = roleIds[k];
        //                     for (var i = 0, cnt = self.tableDataRole.length; i < cnt; i++) {
        //                         var crow = self.tableDataRole[i];
        //                         if (rid == self.tableDataRole[i].id) {
        //                             self.$refs.roleTable.toggleRowSelection(crow);
        //                             break;
        //                         }
        //                     }
        //                 }
        //             }
        //         });
        // },
        initTable: function () {
            var self = this;
            Util.postJson('/sysUser/listLikeUserRoleByPage?'+ "currentPage=" + this.paramData.currentPage
            +"&pageSize="+this.paramData.pageSize, this.paramData,function (result) {
                if (result.success) {
                    console.log(result.obj.records)
                    self.tableData = result.obj.records;
                    self.paramData.total = result.obj.total;
                }
            });
        },
        initTableRole: function () {
            var vm = this;
            // var tdr = this.tableDataRole;
            Util.post('/sysRole/roleUser',{},function (result) {
                if (result.success) {
                    console.log(result.data);
                    vm.tableDataRole = result.data;
                }
            });
        },
        // loadRoleMenu: function (row) {
        //     var self = this;
        //     Util.post('/system/rightmenu/listRoleMenus', {roleId: row.id}, function (result) {
        //         if (result.success) {
        //             self.$refs.menuTree.setCheckedKeys(result.obj.menuCodes || []);
        //         }
        //     });
        // },
        // initTree: function () {
        //     var self = this;
        //     Util.postEncrypt('/system/menu/listAllMenuTreeForAuth', {disabled: true}, function (result) {
        //         if (result.success) {
        //             self.menuTreeData = [result.obj];
        //         }
        //     });
        // },
        statusRender: function (value) {
            return this.statusDic[value];
        },
        roleTypeRender: function (value) {
            return this.roleTypeDic[value] || '自定义';
        }
    }
});

